import { apiCartGoodCount } from 'js/api'

export default {
  state: {
    messageList: [],
    tradeList: [],
    cartCount: 0
  },
  getters: {
    messageList: state => state.messageList,
    tradeList: state => state.tradeList,
    cartCount: state => state.cartCount
  },
  actions: {
    // 获取购物车数量
    fetchCartCount({state}, token) {
      if (token) {
        apiCartGoodCount(token).then(data => {
          state.cartCount = data.goodsCount || 0
        })
      }
    }
  },
  mutations: {
    updateMsgList(state, payload) {
      state.messageList = payload
    },
    updateTradeList(state, payload) {
      state.tradeList = payload.map(item => {
        return {
          id: item.id,
          tradeName: item.tradeName
        }
      })
    },
    clearInfoByClickExit(state) {
      state.messageList = []
      state.cartCount = 0
    }
  }
}
